
class HhdzProxy extends ResourceProxyBase {
    public static NAME: string = "HhdzProxy";//必须和excel导出文件一致
    public static Proxy: HhdzProxy = null;
    public constructor() {
        super(HhdzProxy.NAME);
    }
    public static getProxy() {
        if (!HhdzProxy.Proxy) {
            HhdzProxy.Proxy = new HhdzProxy();
        }
        return HhdzProxy.Proxy;
    }

    //登录成功
    public login(data: Object) {
        HhdzVO.VO = null;
        HhdzVO.getVo().lUserScore = GameVO.getVo().lUserScore;
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.OPEN_PLAY_GAME);

    }
    //获取房间配置信息，获取完成后发送匹配玩家请求
    public getRoomInfo(data: Object) {
        HhdzVO.getVo().dwServerRule = data["dwServerRule"];
        HhdzVO.getVo().wChairCount = data["wChairCount"];
        HhdzVO.getVo().wServerType = data["wServerType"];
        HhdzVO.getVo().wTableCount = data["wTableCount"];
    }
    //用户进入广场 携带用户所有数据 3-100
    public userJoinSquare(data: Object) {
        let user = data["tagMobileUserInfoHead"];
        HhdzVO.getVo().userInfo[user.dwUserID] = user;;//广场所有人存入集合
        this.userJoinRoom();
    }
    //用户战绩信息刷新 包括金币 3-101
    public getUserInfo(data: Object) {
        let UserScore = data["UserScore"];
        let dwUserID = data["dwUserID"];
        let lScore = UserScore["lScore"];
        let user = HhdzVO.getVo().userInfo[dwUserID];
        if (user) {
            user.lScore = lScore;
        }
        this.userJoinRoom();
    }
    //用户状态变更 入座，离开 3-102
    public userStateModify(data: Object) {
        //用户状态发生改变从缓存的所有用户中找到该用户改变其座位
        let dwUserID = data["dwUserID"];
        let wTableID = data["tableId"];
        let wChairID = data["wChairID"];
        let cbUserStatus = data["cbUserStatus"];
        let user = HhdzVO.getVo().userInfo[dwUserID]
        if (user) {
            user.wTableID = wTableID;
            user.wChairID = wChairID;
            user.cbUserStatus = cbUserStatus;
        }
        this.userJoinRoom();
    }

    public userJoinRoom() {
        HhdzVO.getVo().initUserInof();
        //状态改变完毕后从所有玩家中找自己是否已入座，若自己入座，则找是否跟自己同桌的用户，将其显示到桌子对面
        let user = HhdzVO.getVo().userInfo[GameVO.getVo().dwUserID]
        if (user) {
            HhdzVO.getVo().wTableId = user.wTableID;
            HhdzVO.getVo().wChairId = user.wChairID;
            HhdzVO.getVo().status = user.cbUserStatus
            HhdzVO.getVo().lUserScore = user.lScore
        }

        //我入座找对手
        if (HhdzVO.getVo().wTableId != null && HhdzVO.getVo().wTableId != 65535) {
            HhdzVO.getVo().wTableId_up = HhdzVO.getVo().wTableId;
            //根据我所在椅子确定其他玩家在桌面坐哪个位置
            for (let i in HhdzVO.getVo().userInfo) {
                let user = HhdzVO.getVo().userInfo[i]
                if (user.wTableID == HhdzVO.getVo().wTableId &&
                    user.dwUserID != GameVO.getVo().dwUserID) {//跟我坐一桌的就是对手
                    HhdzVO.getVo().playerNumber++;
                }
            }
            game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.JOIN_ROOM);
        } else {
            if (HhdzVO.getVo().wTableId_up != null && HhdzVO.getVo().wTableId == 65535) {
                //上次有座位此次没座位说明被踢了
                SocketManager.closeSocket(InstructionsEnum.GAME_TYPE_NAME.HHDZ);
            }

        }
    }
    //游戏开始
    public gameStart(data: Object) {
        HhdzVO.getVo().initGameInfo()
        HhdzVO.getVo().game_state = 1
        HhdzVO.getVo().gameNum++;
        HhdzVO.getVo().cbTimeLeave = data["cbTimeLeave"];       //剩余时间
        HhdzVO.getVo().wBankerUser = data["wBankerUser"];       //庄家用户
        HhdzVO.getVo().lBankerScore = data["lBankerScore"];        //庄家金币
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.START_GAME);
    }
    //游戏空闲
    public gameFree(data: Object) {
        HhdzVO.getVo().cbTimeLeave = data["cbTimeLeave"];       //剩余时间
        HhdzVO.getVo().status = 0
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.FREE_GAME);
    }

    //100-101 中途加入
    public dataRecovery(data: Object) {
        if (RegDataLhd.MainCMD_MsgId[100][101] == RegDataLhd.gameStart) {
            HhdzVO.getVo().initGameInfo()
            HhdzVO.getVo().cbTimeLeave = data["cbTimeLeave"] //剩余时间
            HhdzVO.getVo().status = data["cbGameStatus"] //游戏状态
            if (HhdzVO.getVo().status == 100) {
                var lAllBet: Array<any> = data["lAllBet"]//所有区域已下注
                var lPlayFreeSocre: Array<any> = data["lPlayBet"] //玩家下注
                HhdzVO.getVo().betNumAdds_1 = lAllBet[1]
                HhdzVO.getVo().betNumAdds_3 = lAllBet[0]
                HhdzVO.getVo().betNumAdds_5 = lAllBet[2]
                HhdzVO.getVo().myBetNumAdds_1 = lPlayFreeSocre[1]
                HhdzVO.getVo().myBetNumAdds_3 = lPlayFreeSocre[0]
                HhdzVO.getVo().myBetNumAdds_5 = lPlayFreeSocre[2]
                HhdzVO.getVo().betNumAdds_my = 0;
                for (let i in lPlayFreeSocre) {
                    HhdzVO.getVo().betNumAdds_my += lPlayFreeSocre[i];
                }
                game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.DATA_RECOVERY);
            }
            else {
                game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.FREE_GAME);

            }

        }
        if (RegDataLhd.MainCMD_MsgId[100][101] == RegDataLhd.free) {
            HhdzVO.getVo().cbTimeLeave = data["cbTimeLeave"]
            game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.FREE_GAME);
        }
    }
    //获取牌局编号
    public getSerialNumber(data: Object) {
        HhdzVO.getVo().gameNumbering = data["szSerialNumber"];
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.GET_SERIAL_NUMBER)
    }

    //下注返回
    public betReceive(data: Object) {

        let wChairID = data["wChairID"];       //用户位置
        let cbBetArea = data["cbBetArea"];        //筹码区域
        let lBetScore = data["lBetScore"];        //加注数目
        HhdzVO.getVo().betUser = wChairID;
        HhdzVO.getVo().betAdds = cbBetArea;
        HhdzVO.getVo().betNum = lBetScore;
        if (wChairID == HhdzVO.getVo().wChairId) {
            HhdzVO.getVo().game_state = 2
            HhdzVO.getVo().gameNum = 0;
            HhdzVO.getVo().lUserScore -= lBetScore;
            let user = HhdzVO.getVo().userInfo[GameVO.getVo().dwUserID];
            if (user) {
                user.lScore = HhdzVO.getVo().lUserScore;
            }
            if (cbBetArea == 0) {
                HhdzVO.getVo().myBetNumAdds_1 += lBetScore;
            }
            if (cbBetArea == 1) {
                HhdzVO.getVo().myBetNumAdds_3 += lBetScore;
            }
            if (cbBetArea == 2) {
                HhdzVO.getVo().myBetNumAdds_5 += lBetScore;
            }
        }
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.BET_RECEIVE);
    }
    //清除路单
    public clearLudan() {
        HhdzVO.getVo().ludan = [];
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.GAME_LUDAN);
    }
    //路单数据
    public ludanInit(data: any) {
        HhdzVO.getVo().ludan = data["data"]
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.GAME_LUDAN);

    }
    //游戏结束
    public gameOver(data: Object) {

        // let cbCardCount: Array<any> = data["cbCardCount"];//两家各自牌数量
        let cbTableCardArray: Array<Array<any>> = data["cbTableCardArray"];//两家的牌，上面数量是几，这里就从数组中取几个
        let cbWinArea: Array<any> = data["cbWinArea"];//所有区域得分
        let lPlayScore: Array<any> = data["lPlayScore"];//玩家在所有区域输赢
        HhdzVO.getVo().cbTimeLeave = data["cbTimeLeave"];
        HhdzVO.getVo().result_xian = data["cbBanker"];
        HhdzVO.getVo().result_zhuang = data["cbPlayer"];
        HhdzVO.getVo().result_xingyun = data["cbTie"];
        HhdzVO.getVo().result_paixing = data["cbType"];
        HhdzVO.getVo().cbAllType = data["cbAllType"];

        HhdzVO.getVo().poker_xian = cbTableCardArray[0].slice(0, 3)
        HhdzVO.getVo().poker_zhuang = cbTableCardArray[1].slice(0, 3)
        HhdzVO.getVo().score_arr = cbWinArea;
        // for (let i in lPlayScore) {
        //     HhdzVO.getVo().score_my += lPlayScore[i];
        // }
        HhdzVO.getVo().score_my = data["lPlayAllScore"];
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.GAME_OVER);

    }
    //推送场外人员
    public receiveUserList(data: Object) {
        var structUserList = data["structUserList"]
        HhdzVO.getVo().structUserList = structUserList;
        var obj: Object = {
            lTakeMinScore: HhdzVO.getVo().lUserScore,
            szCityName: GameVO.getVo().szNickName,
            wFaceID: GameVO.getVo().wFaceID
        }
        HhdzVO.getVo().structUserList.push(obj)
        game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.RECEIVE_USER_LIST);
    }
}
